package com.cddx.logistics.controller;

import com.cddx.logistics.domain.dto.QueryDTO;
import com.cddx.logistics.domain.dto.UserLoginDTO;
import com.cddx.logistics.domain.dto.UserRoleDTO;
import com.cddx.logistics.domain.pojo.User;
import com.cddx.logistics.service.UserService;
import com.cddx.logistics.util.model.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/queryPage")
    @ApiOperation("分页查询用户")
    public R queryPage(@RequestBody QueryDTO queryDTO){
        return R.ok(userService.queryPage(queryDTO));
    }
    @PostMapping("/add")
    @ApiOperation("添加用户")
    public R addUser(@RequestBody User user){
        return userService.addUser(user)?R.ok():R.error("用户已存在||添加失败");
    }
    @PostMapping("/update")
    @ApiOperation("修改用户")
    public R updateUser(@RequestBody User user){
        return userService.updateUser(user)?R.ok():R.error("用户不存在||修改失败");
    }
    @GetMapping("/delete/{id}")
    @ApiOperation("删除用户")
    public R deleteUser(@PathVariable Integer id){
        return userService.deleteUser(id)?R.ok():R.error("用户不存在||删除失败");
    }

    @PostMapping("/addRole")
    @ApiOperation("添加用户角色")
    public R addRole(@RequestBody UserRoleDTO userRoleDTO){
        return userService.addRole(userRoleDTO)?R.ok():R.error("添加失败");
    }
    @GetMapping("/updateUserStatus")
    @ApiOperation("修改用户状态")
    public R updateUserStatus(Integer id,Integer status){
        return userService.updateUserStatus(id,status)?R.ok():R.error("修改失败");
    }

    //批量删除用户
    @PostMapping("/deleteIds")
    @ApiOperation("批量删除用户")
    public R deleteIds(@RequestBody Integer[] ids){
        return userService.deleteIds(ids)?R.ok():R.error("删除失败");
    }

    @GetMapping("/queryRole/{id}")
    @ApiOperation("查询用户角色")
    public R queryRole(@PathVariable Integer id){
        return R.ok(userService.queryRole(id));
    }

    @PostMapping("/deleteRole")
    @ApiOperation("删除用户角色")
    public R deleteRole(@RequestBody UserRoleDTO userRoleDTO){
        return userService.deleteRole(userRoleDTO)?R.ok():R.error("删除失败");
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R login(@RequestBody UserLoginDTO userLoginDTO){
        User user=userService.login(userLoginDTO);
        return user!=null?R.ok(user):R.error("用户不存在||密码错误");
    }
    @GetMapping("/userInfo}")
    @ApiOperation("获取用户信息")
    public R userInfo(@Param("account")String account){
        return R.ok(userService.userInfo(account));
    }
}
